/*
 * @Description:
 * @Author: xuzp4
 * @Date: 2022-03-22 08:54:13
 * @LastEditors: xuzp4
 * @LastEditTime: 2022-08-15 21:41:28
 * @FilePath: \visitor_wechat\src\services\index.js
 */
import axios from 'axios'
import store from '../store/index'

/** 员工接口 */

// 员工——接口请求统一封装
function httpRequest ({url, method = 'post', params = {}}) {
  const tokenVal = store.state.token
  // const sign = signMd5Utils.getSign(url, params)
  const configParam = {
    url,
    method,
  }
  if (method.toLowerCase() === 'get') {
    configParam.params = params
  } else if (method.toLowerCase() === 'postquery') {
    configParam.method = 'post'
    configParam.params = params
  } else {
    configParam.data = {
      ...params
    }
  }
  if (!tokenVal) {
  } else {
    configParam.headers['X-Access-Token'] = tokenVal
  }
  return axios(configParam).then(res => {
    if (res.status === 200) {
      return res.data
    } else if (res.status === 401) {
      // getTokenServer().then(() => {
      //   return httpRequest({ url, method, params })
      // })
    } else {
      throw res
    }
  }).catch(error => {
    if (error.message.indexOf('401') !== -1) {
      sessionStorage.clear()
      localStorage.clear()
      // const currentTime = new Date().getTime();
      // const oauthUri = `${getSsoLoginUri()}?clientId=${getSsoLoginClientId()}&redirectUri=${getSsoRedirectUri()}&state=${currentTime}`;
      // location.href = oauthUri
    } else {
      throw error
    }
  })
}
